/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.core.common.utils.CryptoUtil
   Module Description   :

   Date Created      : 2011/8/31
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.core.common.utils;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author cut7man
 * 
 */
public class CryptoUtil {

	public static byte[] aesEncrypt(byte[] key, byte[] msg) throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

		SecretKeySpec spec = new SecretKeySpec(key, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.ENCRYPT_MODE, spec);
		return cipher.doFinal(msg);
	}

	public static byte[] aesDecrypt(byte[] key, byte[] msg) throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

		SecretKeySpec spec = new SecretKeySpec(key, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.DECRYPT_MODE, spec);
		return cipher.doFinal(msg);
	}

	public static byte[] hexAseDecrypt(String key, String msg) {
		String source = msg;
		int charCount = msg.length() / 2;

		String strHex = "";
		int intHex = 0;
		byte byteData[] = new byte[charCount];
		for (int i = 0; i < charCount; i++) {
			strHex = source.substring(0, 2);
			source = source.substring(2);
			intHex = Integer.parseInt(strHex, 16);
			if (intHex > 128) {
				intHex = intHex - 256;
			}
			byteData[i] = (byte) intHex;
		}

		return byteData;
	}

	public static String hexAseEncrypt(String key, byte[] msg) {
		StringBuffer sb = new StringBuffer("");
		int intHex = 0;

		for (byte b : msg) {
			String strHex = "";
			String strReturn = "";
			intHex = b;
			if (intHex < 0) {
				intHex += 256;
			}
			if (intHex < 16) {
				strHex += "0" + Integer.toHexString(intHex).toUpperCase();
			} else {
				strHex += Integer.toHexString(intHex).toUpperCase();
			}

			strReturn = strHex;
			sb.append(strReturn);
			// sb.append(Integer.toHexString(b));

			// System.out.println("byte:" + b);
		}
		return sb.toString();
	}

	public static void main(String args[]) {
		String msg = "FAD78B9891F1A3164E715E4F9959E5B6B76D978DE86FD83BB4BA30AB221BA16A942DA2BED57F47A399BFE4CCDFD77619256074D1767B5A68660F404F6B1274F3";

		String key = "yoka_&yaodian100";

		String original = "userId=00000123&userName=中文&timestamp=201109011630";
		try {
			byte[] enc = (CryptoUtil.aesEncrypt(key.getBytes(), original.getBytes()));
			String hexEnc = CryptoUtil.hexAseEncrypt(key, enc);
			System.out.println("enc:" + new String(enc));
			System.out.println("hexEnc:" + hexEnc);
			System.out.println(new String(CryptoUtil.aesDecrypt(key.getBytes(), CryptoUtil.hexAseDecrypt(key, msg))));
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
